// pages/collage/collage.js
var app=getApp();
var set;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _num:2,
    set:''
  },

  //转时间
  uctime: function () {
    console.log('加载定时器')
    let that = this;
    let data = that.data.goods;
    for (var i = 0; i < data.length;i++){
      if (data[i].outtime <= 0){
        wx.request({
          url: app.data.Url + '/spell/SpellTimeOut',
          data: {
            suijizhi: data[i].suijizhi,
            sid: data[i].sid
          },
          success: function (res) {
            var data = '';
            //console.log(res.data.data)
          }
        })
      }else{
        var time = data[i].outtime
        var t;
        if (time == 0) {
          clearInterval(that.data.set)
          that.onShow()
          console.log('结束')
        } else {
          let t;
          time = time - 1
          if (time > 1) {
            var day = Math.floor(time / 3600 / 24);     //天
            //console.log(day,'------------')
            var hour = Math.floor(time / 3600) % 24;       //时
            //console.log(hour,'hourhour')
            var min = Math.floor(time / 60) % 60;     //分
            var sec = time % 60;                      //秒
            t = day + '天'
            if (hour < 10) {
              t += '0' + hour + "时";
            } else {
              t += hour + "时";
            }
            if (min < 10) { t += "0"; }
            t += min + "分";
            //console.log(t, 'ttttttttt')
            if (sec < 10) { t += "0"; }
            t += sec      //.toFixed(2);
          }
          data[i].outtime = data[i].outtime-1
          data[i].outtimes = t
          that.setData({
            goods: data
          })
        }
      }
    }
  },

  focus_click: function (e) {
    var that = this;
    clearInterval(that.data.set)          //开始清除定时器
    wx.showLoading({                      //加载滚动条
      title: '加载中',
    })
    that.setData({
      goods: []
    })
    this.setData({
      _num: e.target.dataset.num
    })
    if(e.target.dataset.num == 1){
      wx.request({
        url: app.data.Url + '/spell/userdefault',
        data: {
          uid: wx.getStorageSync('userInfo').uid,
          type: that.data._num
        },
        success: function (res) {
          var data = '';
          that.setData({
            goods: []
          })
          //console.log(res.data.data)
          if (res.data.data != '') {
            console.log('true')
            data = res.data.data
            that.setData({
              goods: data
            })
            that.setData({
              set: setInterval(function () { that.uctime() }, 1000)
            })
          } else {
            that.setData({
              goods: []
            })
            console.log('falses')
          }
        }
      })
    } else if (e.target.dataset.num == 3){
      wx.request({
        url: app.data.Url + '/spell/userdefault',
        data: {
          uid: wx.getStorageSync('userInfo').uid,
          type: that.data._num
        },
        success: function (res) {
          var data = '';
          //console.log(res.data.data)
          if (res.data.data != '') {
            console.log('true')
            data = res.data.data
            for (var i = 0; i < data.length;i++){
              if (data[i].status == 2 && data[i].s_status == 1){
                data[i].ntext = '拼团成功 待发货'
              }
              if (data[i].status == 3 && data[i].s_status == 1) {
                data[i].ntext = '拼团成功 已发货'
              }
            }
            that.setData({
              goods: data
            })
          } else {
            that.setData({
              goods: []
            })
            console.log('falses')
          }
        }
      })
    }else{
      wx.request({
        url: app.data.Url + '/spell/userdefault',
        data: {
          uid: wx.getStorageSync('userInfo').uid,
          type: that.data._num
        },
        success: function (res) {
          var data = '';
          //console.log(res.data.data)
          if (res.data.data != '') {
            console.log('true')
            data = res.data.data
            that.setData({
              goods: data
            })
          } else {
            that.setData({
              goods: []
            })
            console.log('falses')
          }
        }
      })
    }
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)
    

  },

  share:function(e){
    wx.navigateTo({
      url: '../share/index?id=' + e.currentTarget.dataset.id,
    })
  },



  //拼团
  pingtuan:function(e){
    app.navTo('/pages/super/super')
  },
  //跳商品详情
  onurl:function(e){
    //../collageWare/collageWare?id=14&goodsid=428007
    //app.navTo('../collageWare/collageWare?goodsid=' + e.currentTarget.dataset.id + '&id=' + e.currentTarget.dataset.sid)
    app.navTo('../order_details/index?id=' + e.currentTarget.dataset.id + '&sid=' + e.currentTarget.dataset.sid + '&type=1')
    
  },
  //评价
  pingjia:function(e){
    var that=this;
    var url = '../reportEvaluate/reportEvaluate?orderid=' + e.currentTarget.dataset.id+'&type=1'
    app.navTo(url)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var index = options.index;
    if (index != null || index != undefined){
      that.setData({
        _num:index
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //wx.showLoading({})
    wx.request({
      url: app.data.Url + '/spell/userdefault',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        type: that.data._num
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.data.length<0){

        }else{
          that.setData({
            goods: res.data.data,
          })
          if (that.data._num == 1) {
            that.setData({
              set: setInterval(function () { that.uctime() }, 1000)
            })
            that.uctime()
          }
        }
        
        
        
        //wx.hideLoading()
      }
    })
  },
  buy:function(e){
    var order_no = e.currentTarget.dataset.id;
    var that=this;
  
    wx.request({
      url: app.data.Url + 'order/orderPayParams',
      data: {
        order_no: order_no,
        title: '智慧社区订单支付',
        uid: wx.getStorageSync('userInfo').uid
      },
      method: 'post',
      success: function (res) {
      
        wx.requestPayment({
          'timeStamp': res.data.data.weixin.timeStamp,
          'nonceStr': res.data.data.weixin.nonceStr,
          'package': res.data.data.weixin.package,
          'signType': 'MD5',
          'paySign': res.data.data.weixin.paySign,
          'success': function (res) {
            wx.request({
              url: 'https://zhapp.4hl.cn/app/pay/notify_urls',//app.data.Url + 'app/pay/notify_urls',
              data: {
                order_no: order_no,
                type: 1
              },
              method: 'post',
              success: function (res) {
              }
            })
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 1000
            })
            that.onShow();
          },
          'fail': function (res) {
            wx.showToast({
              title: '支付失败',
              icon: 'success',
              duration: 1000
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('监听页面隐藏')
    clearInterval(this.data.set)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    if (e.from === 'button') {
      
      // 来自页面内转发按钮
      console.log(e.target)
    }
    return {
      title: '广电智慧Life小程序',
      path: 'pages/share/index?id=' + e.target.dataset.id,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
        })
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
        })
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },
  outbuy:function(e){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '您确定取消订单吗',
      success:function(res){
        if(res.confirm==1){
          wx.showLoading({})
          wx.request({
            url: app.data.Url+'spell/delOrder',
            data:{
              order_no: e.currentTarget.dataset.id
            },
            method:'post',
            success:function(rs){
              wx.hideLoading();
              that.onShow();
              wx.showToast({
                title: '取消成功',
              })
            }
          })
        }
      }
    })
  }
})